package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.PinYinUtil;
import cn.stylefeng.guns.sys.modular.system.entity.ZxHuopin;
import cn.stylefeng.guns.sys.modular.system.entity.ZxHuopinGsh;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemuKey;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxBxMingxiMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxChengbenMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxHuopinMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxKemuKeyMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxChengbenParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxHuopinParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxHuopinResult;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxKemuKeyResult;
import cn.stylefeng.guns.sys.modular.system.service.ZxHuopinGshService;
import cn.stylefeng.guns.sys.modular.system.service.ZxHuopinService;
import cn.stylefeng.guns.sys.modular.system.service.ZxKemuKeyService;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 装修货品表 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2020-11-02
 */
@Service
public class ZxHuopinServiceImpl extends ServiceImpl<ZxHuopinMapper, ZxHuopin> implements ZxHuopinService {

    @Resource
    private ZxChengbenMapper zxChengbenMapper;

    @Resource
    private ZxBxMingxiMapper zxBxMingxiMapper;
    @Resource
    private ZxKemuKeyService zxKemuKeyService;
    @Resource
    private ZxHuopinGshService gshService;
    @Override
    public void add(ZxHuopinParam param){
        param.setZhujima(PinYinUtil.getPinYinCode(param.getName()));
        param.setCreateUser(LoginContextHolder.getContext().getUserId());
        param.setCreateUserName(LoginContextHolder.getContext().getUser().getName());

        //如果没有科目，需要根据关键字去取一下
//        if(param.getBtyZxKemuId()==null||param.getBtyZxKemuId().equals("")){
//            String nm = param.getName();
//            ZxKemuKeyResult r =zxKemuKeyService.findByKeywords(nm);
//            if(r!=null){
//                param.setBtyZxKemuId(r.getBtyZxKemuId());
//            }
//        }

        ZxHuopin entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public ResponseData delete(ZxHuopinParam param){
        //如果该货品已经被成本所用，不可删除
        ZxChengbenParam zxChengbenParam=new ZxChengbenParam();
        zxChengbenParam.setBtyZxHuopinId(param.getBtyZxHuopinId());
        Integer count=zxChengbenMapper.selectCountByHuoPinId(zxChengbenParam);
        if(count!=null && count>0){
            return new ErrorResponseData("删除货品失败，该货品已经在装修成本中使用");
        }
//        this.removeById(getKey(param));

        ZxHuopin zxHuopin = this.getById(getKey(param));
        //逻辑删除
        zxHuopin.setDelFlag(1);
        zxHuopin.setUpdateUser(LoginContextHolder.getContext().getUserId());
        zxHuopin.setUpdateUserName(LoginContextHolder.getContext().getUser().getName());
        this.updateById(zxHuopin);

        return new SuccessResponseData();
    }

    @Override
    public void update(ZxHuopinParam param){
        //修改是重新获取一下
//        String nm = param.getName();
//        ZxKemuKeyResult r =zxKemuKeyService.findByKeywords(nm);
//        if(r!=null){
//            param.setBtyZxKemuId(r.getBtyZxKemuId());
//        }


        ZxHuopin oldEntity = getOldEntity(param);
        ZxHuopin newEntity = getEntity(param);
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        newEntity.setUpdateUserName(LoginContextHolder.getContext().getUser().getName());
        ToolUtil.copyProperties(newEntity, oldEntity);

        this.updateById(newEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGhs(ZxHuopinParam param){
        ZxHuopin oldEntity = getOldEntity(param);
        ZxHuopin newEntity = getEntity(param);
        newEntity.setUpdateUser(LoginContextHolder.getContext().getUserId());
        newEntity.setUpdateUserName(LoginContextHolder.getContext().getUser().getName());
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        //同时保存到货品供货商对应关系表
//        if(ToolUtil.isNotEmpty(param.getGhsIds())){
//            String ghsIds[]=param.getGhsIds().split(",");
//            //清除之前的数据重新添加
//            List<ZxHuopinGsh>list=gshService.list(new QueryWrapper<ZxHuopinGsh>().eq("zx_huopin_id",param.getBtyZxHuopinId()));
//            if(list.size()>0){
//                gshService.remove(new QueryWrapper<ZxHuopinGsh>().eq("zx_huopin_id",param.getBtyZxHuopinId()));
//            }
//            for (int i = 0; i <ghsIds.length ; i++) {
//                ZxHuopinGsh gsh=new ZxHuopinGsh();
//                gsh.setZxHuopinId(param.getBtyZxHuopinId());
//                gsh.setGshId(Long.valueOf(ghsIds[i]));
//                gsh.setCreateUser(LoginContextHolder.getContext().getUserId());
//                gshService.save(gsh);
//            }
//        }
    }

    @Override
    public ZxHuopinResult findBySpec(ZxHuopinParam param){
        return this.baseMapper.findBySpec(param);
    }

    @Override
    public List<ZxHuopinResult> findListBySpec(ZxHuopinParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ZxHuopinParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZxHuopinResult> ghsPageList(ZxHuopinParam param){
        List<ZxHuopinResult> list = this.baseMapper.ghsPageList(param);
        return list;
    }

    private Serializable getKey(ZxHuopinParam param){
        return param.getBtyZxHuopinId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ZxHuopin getOldEntity(ZxHuopinParam param) {
        return this.getById(getKey(param));
    }

    private ZxHuopin getEntity(ZxHuopinParam param) {
        ZxHuopin entity = new ZxHuopin();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    @Override
    public List<Map<String,Object>> listHuopinSelect(){
            return this.baseMapper.listHuopinSelect();
    }

    @Override
    public void update(String btyZxHuopinId, Long btyZxKemuId, String btyZxKemuIdView) {
        this.getBaseMapper().updateKemu(btyZxHuopinId,btyZxKemuId,btyZxKemuIdView);
        this.zxBxMingxiMapper.tongbuKemu(btyZxHuopinId);
    }

    @Override
    public void updateJgc(String btyZxHuopinId, String isJgc) {
        this.getBaseMapper().updateJgc(btyZxHuopinId,isJgc);
    }

    @Override
    public String add1(ZxHuopinParam param){
        param.setZhujima(PinYinUtil.getPinYinCode(param.getName()));
        param.setCreateUser(LoginContextHolder.getContext().getUserId());
        param.setCreateUserName(LoginContextHolder.getContext().getUser().getName());

        ZxHuopin entity = getEntity(param);
        this.save(entity);
        return entity.getBtyZxHuopinId();
    }

    @Override
    public List<ZxHuopin> selectListByName(ZxHuopinParam param) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("name", param.getName());
        qw.eq("hp_type", 2);
        return this.baseMapper.selectList(qw);
    }
}
